package com.itheima.controller;

import com.itheima.pojo.PageAll;
import com.itheima.pojo.Student;
import com.itheima.pojo.entity.Result;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getAllStudents(@RequestParam(required = false) String name,
                                 @RequestParam(required = false) Integer degree,
                                 @RequestParam(required = false) Long clazzId,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        PageAll pageAll = studentService.getAllStudents(name, degree, clazzId, page, pageSize);
        log.info("传入分页条件name:{},degree:{},clazzId:{},page:{},PageSize", name, degree, clazzId, page, pageSize);
        return Result.success(pageAll);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        Student student = studentService.getById(id);
        log.info("传入学生id:{}", id);
        return Result.success(student);
    }

    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        log.info("传入学生对象:{}", student);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Long> ids) {
        studentService.deleteStudent(ids);
        log.info("传入学生id集合：{}",ids);
        return Result.success();
    }

    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        log.info("传入学生对象：{}",student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateScoreById(@PathVariable Long id,
                                  @PathVariable Integer score) {
        studentService.updateScoreById(id,score);
        log.info("传入学生id：{},传入扣除分数：{}",id,score);
        return Result.success();
    }
}
